package ts.group.webapp.market.dao;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Repository;

import ts.group.webapp.market.common.core.util.ResponseData;
import ts.group.webapp.market.model.TUser;

import com.starit.common.dao.hibernate4.HibernateBaseDaoImpl;

@Repository
public class IUserDao extends HibernateBaseDaoImpl<TUser,Long >{
	
	public ResponseData getLoginModel(HttpServletRequest request, HttpServletResponse response, HttpSession session, TUser user, String username , String password){
		
		String hql = "from TUser t where t.username = ?";
		List<TUser> userList = this.findByHQL(hql,username);
		if(userList.size() == 0) {
			response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
			return new ResponseData(false, "UsernameNotFound", "username doesn not exist.");
		} else if(!password.equals(userList.get(0).getPassword())) {
			response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
			return new ResponseData(false, "BadCredentials", "password is not correct.");
		} else {
			user.setUsername(username);
			user.setPassword(password);
			//HttpSession session = request.getSession();
			session.setAttribute("__SESSIONKEY__", user);
			return ResponseData.SUCCESS_NO_DATA;
		}
	}
}
